package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketRegionExample;
import com.cskaoyan.market.db.mapper.MarketRegionMapper;
import com.cskaoyan.market.service.admin.MarketRegionService;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.vo.admin.MarketRegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MarketRegionServiceImpl implements MarketRegionService {

    @Autowired
    MarketRegionMapper regionMapper;
    /**
     * @MethodName getMarketRegionList
     * @Description 对省市县数据进行打包返回
     * @return: java.util.List<com.cskaoyan.market.vo.admin.MarketRegionVo>
     * @Author M
     * @Date 14:07 2023/5/8
     **/
    @Override
    public List<MarketRegionVo> getMarketRegionList() {

        // 分别查询三次，获得省、市、县 表
        // 市的查询sql
        MarketRegionExample provinceExample = new MarketRegionExample();
        provinceExample.or().andTypeEqualTo(MarketConstant.PROVINCE_TYPE);
        // 市
        MarketRegionExample cityExample = new MarketRegionExample();
        cityExample.or().andTypeEqualTo(MarketConstant.CITY_TYPE);
        // 区
        MarketRegionExample areaExample = new MarketRegionExample();
        areaExample.or().andTypeEqualTo(MarketConstant.AREA_TYPE);
        // 获取相应表
        List<MarketRegion> provinceRegions = regionMapper.selectByExample(provinceExample);
//        sqlSession.commit();
        List<MarketRegion> cityRegions = regionMapper.selectByExample(cityExample);
//        sqlSession.commit();
        List<MarketRegion> areaRegions = regionMapper.selectByExample(areaExample);

        //新建一个map，在里面填充三组键值对，键为行政区域的type类型，value是一个空list
        Map<Byte, List<MarketRegion>> regionsGroupByType = new HashMap<>();
        regionsGroupByType.put(MarketConstant.PROVINCE_TYPE, provinceRegions);
        regionsGroupByType.put(MarketConstant.CITY_TYPE, cityRegions);
        regionsGroupByType.put(MarketConstant.AREA_TYPE, areaRegions);

        //将所有的市信息，按照pid也就是省来进行分组
        Map<Integer, List<MarketRegion>> cityRegionsGroupByPid = new HashMap<>();
        //将所有的区信息，按照pid也就是市来进行分组
        Map<Integer, List<MarketRegion>> areaRegionsGroupByPid = new HashMap<>();

        // 处理省市关系数据
        for (MarketRegion cityRegion : cityRegions) {
            Integer pid = cityRegion.getPid();
            // 获取pid对应的list对象作为cityRegionOfOnePid
            List<MarketRegion> cityRegionOfOnePid = cityRegionsGroupByPid.get(pid);
            // 如果说第一次添加
            if (cityRegionOfOnePid == null) {
                cityRegionOfOnePid = new ArrayList<>();
                cityRegionsGroupByPid.put(pid, cityRegionOfOnePid);
            }
            // 已经有此key对应的list表，直接把数据添加到此list中
            cityRegionOfOnePid.add(cityRegion);
        }

        // 处理市县数据
        for (MarketRegion areaRegion : areaRegions) {
            Integer pid = areaRegion.getPid();
            List<MarketRegion> areaRegionOfOnePid = areaRegionsGroupByPid.get(pid);
            if (areaRegionOfOnePid == null) {
                areaRegionOfOnePid = new ArrayList<>();
                areaRegionsGroupByPid.put(pid, areaRegionOfOnePid);
            }
            areaRegionOfOnePid.add(areaRegion);
        }

        // 用vo信息封装上面得到的数据表
        List<MarketRegionVo> provinceVOS = new ArrayList<>();
        // 遍历每个省份
        for (MarketRegion province : provinceRegions) {
            // 省-》市-》区
            MarketRegionVo provinceVo = new MarketRegionVo();
            provinceVo.setId(province.getId());
            provinceVo.setName(province.getName());
            provinceVo.setType(province.getType());
            provinceVo.setCode(province.getCode());
            // 处理 获得children
            // 首先获取当前省份对应的城市列表
            List<MarketRegion> cities = cityRegionsGroupByPid.get(province.getId());

            // 创建城市vo，封装每个城市的信息
            List<MarketRegionVo> cityVOS = new ArrayList<>();
            for (MarketRegion city : cities) {
                MarketRegionVo cityVo = new MarketRegionVo();
                cityVo.setId(city.getId());
                cityVo.setName(city.getName());
                cityVo.setType(city.getType());
                cityVo.setCode(city.getCode());
                // 获取当前城市对应的区县列表
                List<MarketRegion> areas = areaRegionsGroupByPid.get(city.getId());

                // 创建区县vo，封装其中的信息
                List<MarketRegionVo> areaVOS = new ArrayList<>();
                for (MarketRegion area : areas) {
                    MarketRegionVo areaVo = new MarketRegionVo();
                    areaVo.setId(area.getId());
                    areaVo.setName(area.getName());
                    areaVo.setType(area.getType());
                    areaVo.setCode(area.getCode());
                    areaVOS.add(areaVo);
                }
                // 把每个城市对应的区县列表加入children中
                cityVo.setChildren(areaVOS);
                // 将Vo对象封装到VOS list中
                cityVOS.add(cityVo);
            }
            provinceVo.setChildren(cityVOS);
            provinceVOS.add(provinceVo);
        }
        return provinceVOS;
    }
}
